#include <stdlib.h>

#ifndef MYLIST_H
#define MYLIST_H

typedef struct element{
	void *item;
	struct element *next;
}Element;

//typedef struct element{
//	void *item;
//	struct element *next;
//	struct element *previos;
//}Element;

typedef Element* position;
typedef Element* List;

// init new list
List initList(){
	Element *ls=(Element *)malloc(sizeof(Element));
	if(ls)
		ls->next=NULL;
	else
		ls=NULL;
	return ls;
}

// insert new item after position p
position insertToList(position p, void *item){
	position newp=(Element *)malloc(sizeof(Element));
	if(newp){
		newp->next=p->next;
		p->next=newp;
		newp->item=item;
		//p=p->next;
	}
	else newp=NULL;
	return newp;
}

// delete item with pointer p
position deleteFromList(List ls, position p){
	position tmp=ls;
	while( tmp->next!= p )
		tmp=tmp->next;
	tmp->next=p->next;
	free(p);
	return tmp;
}

// retrive data from item where pointer p
void *retriveFromList(position p){
	return p->item;
}

// next position
position Next(position p){
	return (position)p->next;
}

// is list empty?
int isEmpty(List ls){
	return ls->next==NULL;
}
// free list from memory
void freeList(List ls){
	position pos=ls->next;
	while(ls->next!=NULL)
	{
		pos=ls->next;
		ls->next=pos->next;
		free(pos);
	}
}


#endif
